import {Size, WindowSetPosition, WindowSetSize} from "../../wailsjs/runtime";
import {
    WinMessageHeight,
    WinMessagePoxX,
    WinMessagePoxY,
    WinMessageWidth,
    WinQuickHeight,
    WinQuickWidth
} from "../constant/winSize";
import {Get, GetNumber} from "../../wailsjs/go/dbservice/DbConfigService";
import {
    ConfigKeyScreenScale,
    ConfigKeyWinHeightMaine,
    ConfigKeyWinHeightMessage,
    ConfigKeyWinHeightQuick,
    ConfigKeyWinPosXMain,
    ConfigKeyWinPosXMessage,
    ConfigKeyWinPosYMain,
    ConfigKeyWinPosYMessage,
    ConfigKeyWinWidthMain,
    ConfigKeyWinWidthMessage,
    ConfigKeyWinWidthQuick
} from "../constant/config_keys";
import {GetMousePos, GetScreenSize} from "../../wailsjs/go/service/SystemService";
import {dto} from "../../wailsjs/go/models";
import {WindowsType} from "../store/windows";
import Pos = dto.Pos;

export async function SetSizeByConfig(winType:WindowsType):Promise<Size> {
    let key_size_w=ConfigKeyWinWidthMain
    let key_size_h=ConfigKeyWinHeightMaine
    if(winType == WindowsType.Message){
         key_size_w=ConfigKeyWinWidthMessage
         key_size_h=ConfigKeyWinHeightMessage
    }
    if(winType == WindowsType.QuickTools){
         key_size_w=ConfigKeyWinWidthQuick
         key_size_h=ConfigKeyWinHeightQuick
    }
    // //面板的固定宽高--取自常量
    let w = await GetNumber(key_size_w)
    let h = await GetNumber(key_size_h)
    let panel_width = w == 0 ? WinMessageWidth : w
    let panel_height = h == 0 ? WinMessageHeight : h
     console.log(panel_height,panel_width," ---panel_height,panel_width")
    await WindowSetSize(panel_width, panel_height)
    let siz:Size={
        w:w,
        h:h
    }
    return siz
}




export async function SetPosByConfig(winType:WindowsType):Promise<Pos> {

    if(winType == WindowsType.QuickTools){
        let pos:Pos={
            pos_x:0,
            pos_y:0
        }
        //快捷面板没有保存位置信息
        return  pos
    }


    let key_pox_x=ConfigKeyWinPosXMain
    let key_pox_y=ConfigKeyWinPosYMain

    if(winType == WindowsType.Message){
         key_pox_x=ConfigKeyWinPosXMessage
         key_pox_y=ConfigKeyWinPosYMessage
    }

    // //面板的固定宽高--取自常量
    let x = await GetNumber(key_pox_x)
    let y = await GetNumber(key_pox_y)

    let panel_x = x == 0 ? WinMessagePoxX : x
    let panel_y = y == 0 ? WinMessagePoxY : y
    await WindowSetPosition(panel_x, panel_y)

    let pos:Pos={
        pos_x:panel_x,
        pos_y:panel_y
    }
    return pos
}

export async function SetSizeQuick():Promise<Size> {
    // //面板的固定宽高--取自常量
    let panel_width = 0
    let panel_height = 0
    let w = await GetNumber(ConfigKeyWinWidthQuick)
    let h = await GetNumber(ConfigKeyWinHeightQuick)
    panel_width = w == 0 ? WinQuickWidth : w
    panel_height = h == 0 ? WinQuickHeight : h
    await WindowSetSize(panel_width, panel_height)
    let size:Size={
        w:panel_height,
        h:panel_width
    }
    return size
}


export async function SetLocationByMouse(panel_width:number,panel_height:number){

   // let size =await WindowGetSize()
   //  let panel_width = size.w
   //  let panel_height = size.h


    //鼠标的xy坐标
    let mouse_pos=await GetMousePos()
    let mousex=mouse_pos.pos_x
    let mousey=mouse_pos.pos_y


    //屏幕的尺寸
    let screen_size=await GetScreenSize()
    let screen_width=screen_size.width
    let screen_height=screen_size.height


    // 计算面板起始坐标,一般情况下鼠标的位置位于panel的中心位置，
    // 但是鼠标在屏幕边缘时panel要能自适应，使得面板不能超出屏幕
    //  let panel_start_x = mousex - panel_width / 2;
    let panel_start_x = mousex ;
    let panel_start_y = mousey - panel_height / 2;

    // 调整起始坐标以确保面板不超出屏幕边界
    if (panel_start_x < 0) {
        panel_start_x = 0;
    } else if (panel_start_x + panel_width > screen_width) {
        panel_start_x = screen_width - panel_width;
    }

    if (panel_start_y < 0) {
        panel_start_y = 0;
    } else if (panel_start_y + panel_height > screen_height) {
        panel_start_y = screen_height - panel_height;
    }
    //获取缩放比例，由于WindowSetPosition是按照原始比例来的，所以我们这里再乘以缩放比例
    let scale =await GetScale()
    WindowSetPosition(panel_start_x*scale,panel_start_y*scale)
   // WindowSetSize(panel_width,panel_height)
    console.log(panel_start_x,panel_start_y,panel_width,panel_height,"-----panel_start_x,panel_start_y,panel_width,panel_height value")
}


async function GetScale() {
    let scaleStr = await Get(ConfigKeyScreenScale)
    if(scaleStr == ""){
        return 1
    }

    return parseFloat(scaleStr)/100
}
